﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using BikeBouncer.BusinessLayer;
using BikeBouncer.BusinessLayer.Entities;
using BikeBouncer.BusinessLayer.Enums;

namespace BikeBouncer.Site
{
	public partial class AddNote_ascx : UserControl
	{
		#region Properties

		public Int32 AddedNoteId
		{
			get
			{
				Object o = ViewState["AddedNoteId"];

				if (o == null)
				{
					return 0;
				}

				return Convert.ToInt32(o);
			}
			set
			{
				ViewState["AddedNoteId"] = value;
			}
		}

		public Int32 ItemId
		{
			get
			{
				Object o = ViewState["AddNote_ItemId"];

				if (o == null)
				{
					return 0;
				}

				return Convert.ToInt32(o);
			}
			set
			{
				ViewState["AddNote_ItemId"] = value;
			}
		}
		public Int32 UserId
		{
			get
			{
				Object o = ViewState["AddNote_UserId"];

				if (o == null)
				{
					return 0;
				}

				return Convert.ToInt32(o);
			}
			set
			{
				ViewState["AddNote_UserId"] = value;
			}
		}

		public String AddressPostcode
		{
			get
			{
				return TxtNotePostcode.Text;
			}
			set
			{
				TxtNotePostcode.Text = value;
			}
		}

		public String AddressLocation
		{
			get
			{
				return TxtNoteLocation.Text;
			}
			set
			{
				TxtNoteLocation.Text = value;
			}
		}

		public String AddressCity
		{
			get
			{
				return TxtNoteCity.Text;
			}
			set
			{
				TxtNoteCity.Text = value;
			}
		}

		public String AddressPhone
		{
			get
			{
				return TxtNotePhone.Text;
			}
			set
			{
				TxtNotePhone.Text = value;
			}
		}

		public ItemStatus ItemStatus
		{
			get
			{
				return (ItemStatus)Convert.ToInt32(CboNoteStatus.SelectedItem.Value);
			}
			set
			{
				if (CboNoteStatus.SelectedItem != null)
				{
					CboNoteStatus.SelectedItem.Selected = false;
				}
				
				CboNoteStatus.Items.FindByText(value.ToString()).Selected = true;
			}
		}

		public String Details
		{
			get
			{
				return TxtNoteDetails.Text;
			}
			set
			{
				TxtNoteDetails.Text = value;
			}
		}

		public DateTime ReferenceDate
		{
			get
			{
				return Convert.ToDateTime(TxtNoteDate.Text);
			}
			set
			{
				TxtNoteDate.Text = value.ToShortDateString();
			}
		}

		#endregion

		#region Main methods

		protected void Page_Init(Object sender, EventArgs e)
		{
			foreach (ItemStatus i in Enum.GetValues(typeof(ItemStatus)))
			{
				ListItem item = new ListItem(Enum.GetName(typeof(ItemStatus), i), ((Int32)i).ToString());
				CboNoteStatus.Items.Add(item);
			}
		}

		protected void Page_Load(Object sender, EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				AjaxManager.SetDefaults_AutoCompleteExtender(AutoComplete_TxtNoteCity);
				AjaxManager.SetDefaults_CalendarExtender(Calendar_TxtNoteDate);

				Calendar_TxtNoteDate.SelectedDate = DateTime.Now.AddHours(-4);
				TxtNoteDate.Text = Calendar_TxtNoteDate.SelectedDate.Value.ToShortDateString();

				ValTxtNoteDate.MinimumValue = DateTime.Now.AddYears(-1).ToShortDateString();
				ValTxtNoteDate.MaximumValue = DateTime.Now.ToShortDateString();
			}
		}

		protected void Page_PreRender(Object sender, EventArgs e)
		{
			ScriptManager.RegisterStartupScript(this, typeof(String), "ChangeStatusInit", "AddNote_ChangeStatus(document.getElementById(\"" + CboNoteStatus.ClientID + "\"));", true);
		}

		public void Clear()
		{
			AddedNoteId = 0;
		}

		#endregion

		#region Insert to the database

		protected void ButAddNote_Click(Object sender, EventArgs e)
		{
			BasePage p = Page as BasePage;

			Page.Validate("Note");

			if (Page.IsValid)
			{
				try
				{
					Insert();
				}
				catch (Exception ex)
				{
					p.AppendError(ex);
				}
			}
			else
			{
				p.AppendError(p.GetGlobalString("PageNotValid"));
			}
		}

		private void Insert()
		{
			Note note = new Note();
			Item item = new Item(ItemId);

			note.Item = item;
			note.User = new User(UserId);
			note.Details = TxtNoteDetails.Text;
			note.Address.UserId = UserId;
			note.Address.Postcode = TxtNotePostcode.Text;
			note.Address.Location = TxtNoteLocation.Text;
			note.Address.Phone = TxtNotePhone.Text;
			note.Address.Active = true;
			note.ReferenceDate = Convert.ToDateTime(TxtNoteDate.Text);
			note.IP = LocationManager.RequestIP;
			note.Active = true;

			note.Address.City = City.Fetch(TxtNoteCity.Text);

			if (String.IsNullOrEmpty(note.Address.City.Country))
			{
				note.Address.City.Country = SessionManager.CurrentCountry;
			}

			if (note.Address.HasContents)
			{
				DataAccess.Address_Upsert(note.Address);
			}

			DataAccess.Note_Insert(note);

			UpdateStatus(item);

			AddedNoteId = note.Id;
		}

		private void UpdateStatus(Item item)
		{
			item.Status = ItemStatus;
			item.StatusDate = DateTime.Now;

			DataAccess.Item_Upsert(item);
		}

		#endregion
	}
}